<?php

/**
 * 同步obj_relate_wiki 到es
 */

error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING ^ E_DEPRECATED);
ini_set('display_errors', 1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\wiki\ObjRelateWiki;
use Doraemon\model\Es\MainContent;
use Doraemon\service\es\EsObjRelateWikiSupplement;
use Biz\service\wiki\Wiki;
use Doraemon\tools\Log;

$taskId = (int)$argv[1] ?? 0;
$programStartTime = time();

$maxId = ObjRelateWiki::get('id',
    ['status' => ObjRelateWiki::STATUS_NORMAL, "ORDER" => ['id' => 'DESC'], 'LIMIT' => 1]);
$pageSize = 4000;

$page = ceil($maxId / $pageSize);
$i = 0;
while ($i < $page) {
    $start = $i * $pageSize;
    $end = ($i + 1) * $pageSize;
    $objRelateWikiList = ObjRelateWiki::getByWhere([
        'status' => ObjRelateWiki::STATUS_NORMAL,
        "id[<>]" => [$start, $end],
        "wiki_id[>]" => 0,
    ]);
    $objIds = array_column($objRelateWikiList, 'obj_id');
    $wikiIds = array_column($objRelateWikiList, 'wiki_id');
    $objInfoMap = MainContent::getBatchByIds(array_unique($objIds));
    $wikiInfoMap= Wiki::getWikiBaseInfos(array_unique($wikiIds));
    $paramsData = [];

    if ($objRelateWikiList) {
        $j = 0;
        foreach ($objRelateWikiList as $objRelateWiki) {
            if ($objRelateWiki['id'] % 10 == $taskId) {
                $params = [];
                $params['id'] = $objRelateWiki['id'];
                $params['obj_id'] = $objRelateWiki['obj_id'];
                $params['obj_type'] = $objRelateWiki['obj_type'];
                $params['rel_obj_id'] = $objRelateWiki['rel_obj_id'];
                $params['rel_obj_type'] = $objRelateWiki['rel_obj_type'];
                $params['wiki_id'] = $objRelateWiki['wiki_id'];
                $params['uid'] = $objRelateWiki['uid'];
                $params['type'] = $objRelateWiki['type'];
                $params['status'] = $objRelateWiki['status'];
                $params['admin_uid'] = $objRelateWiki['admin_uid'];

                $params['basic_id'] = $wikiInfoMap[$objRelateWiki['wiki_id']]['basic_id'] ?? 0;
                $params['addtime'] = $objInfoMap[$objRelateWiki['obj_id']]['addtime'] ?? 0;
                $params['admin_score'] = $objInfoMap[$objRelateWiki['obj_id']]['admin_score'] ?? 0;
                $paramsData[] = $params;
                $j++;
            }
            // 批量同步至es
            if (count($paramsData) == 100 ) {
                echo '$paramsData-'.count($paramsData).PHP_EOL;
                if (EsObjRelateWikiSupplement::batchAdd($paramsData) == false) {
                    echo '插入es失败,接口问题'.PHP_EOL;
                    Log::error(json_encode($paramsData));

                    return false;
                }
                $paramsData = [];
            }
        }
        if (!empty($paramsData)) {
            echo '$paramsData-兜底'.count($paramsData).PHP_EOL;
            if (EsObjRelateWikiSupplement::batchAdd($paramsData) == false) {
                echo '插入es失败,接口问题'.PHP_EOL;
                Log::error(json_encode($paramsData));
                return false;
            }
        }
        echo '第'.$i.'批数据同步完成,共同步'.$j.'条数据'.PHP_EOL;
    }
    $i++;
}
$programEndTime = time();
echo '共消耗时间'.($programEndTime - $programStartTime).PHP_EOL;
